﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Edu.Web.Biz;
using Edu.Web.Data.Entity;
using Edu.Web.Data.Service;
using ESoft.Core.Utils.Extensions;

namespace Edu.Web.Controllers
{
    public class ClazzInfoController : BaseController
    {
        private IClazzService _clazzService;
        private IStudentService _studentService;

        public ClazzInfoController(IClazzService clazzService, IStudentService studentService)
        {
            _clazzService = clazzService;
            _studentService = studentService;
        }

        // GET: ClazzInfo
        public ActionResult Index()
        {
            return View();
        }

        // GET: ClazzInfo
        public ActionResult List()
        {
            // var clazzList = _clazzService.LoadClazzByUserId(CurrentUser.UserId, CurrentUser.UserType);
            return View();
        }

        /// <summary>
        /// 获取班级列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetList()
        {
            var clazzList = _clazzService.LoadClazzByUserId(CurrentUser.UserId, CurrentUser.UserType);
            return Json(clazzList, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 班级详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("/info/{id}")]
        public ActionResult Info(long id)
        {
            ViewData["currentId"] = id;
            return View();
        }

        /// <summary>
        /// 获取学生列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetStudentList(long id)
        {
            var studentList = _clazzService.LoadClazzStuByClazzId(id);
            return Json(studentList, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取学生列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetClazzInfo(long id)
        {
            var clazzEntity = _clazzService.Find(id);
            return Json(clazzEntity, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 班级详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("/info/{id}")]
        public ActionResult CreateStudent(long id)
        {
            ViewBag.ClazzId = id;
            return View();
        }

        /// <summary>
        /// 班级详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("/SaveStundent/{id}")]
        [HttpPost]
        public ActionResult SaveStundent(long id)
        {
            try
            {
                /**
                 *  user.UserName = userStudent["StuNo"].ToString();
                    user.Gender = Convert.ToInt32(userStudent["Gender"]);
                    user.RealName = userStudent["RealName"].ToString();
                    user.UserPassword = userStudent["StuNo"].ToString();
                 * */
                Dictionary<String, Object> dic = new Dictionary<string, object>();
                dic["StuNo"] = base.GetRequestMap("stuNo", "");
                dic["Gender"] = Convert.ToInt32(base.GetRequestMap("gender", "1"));
                dic["RealName"] = base.GetRequestMap("realName", "1");
                if (dic["StuNo"].ToString().IsNullOrWhiteSpace())
                {
                    throw new Exception("请输入学号！");
                }
                if (dic["RealName"].ToString().IsNullOrWhiteSpace())
                {
                    throw new Exception("请输入姓名！");
                }
                _studentService.SaveStudentOfClazz(new List<Dictionary<String, Object>> { dic }, id);
                return Content("true");
            }
            catch (Exception e)
            {
                return Content("err:" + e.Message);
            }
        }

        /// <summary>
        /// 创建班级
        /// </summary>
        /// <returns></returns>
        public ActionResult CreateClass()
        {
            return View();
        }

        /// <summary>
        /// 编辑班级
        /// </summary>
        /// <param name="id">班级ID</param>
        /// <returns></returns>
        public ActionResult Edit(long id)
        {
            ViewData["clazzId"] = id;
            var clazzInfo = _clazzService.Find(id);
            return View(clazzInfo);
        }

        /// <summary>
        /// 保存班级
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult saveClazz()
        {
            try
            {
                ClazzEntity clazz = new ClazzEntity();
                clazz.ClazzName = GetRequestMap("clazzName", String.Empty);
                if (clazz.ClazzName.IsNullOrWhiteSpace())
                {
                    throw new Exception("请输入班级名称！");
                }
                long clazzId = _clazzService.SaveClazz(clazz, CurrentUser.UserId);
                return Content(clazzId.ToString());
            }
            catch (Exception e)
            {
                return Content("err:" + e.Message);
            }
        }


        /// <summary>
        /// 保存班级
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdateClazz(long clazzId)
        {
            try
            {
                ClazzEntity clazz = new ClazzEntity();
                clazz.Id = clazzId;
                clazz.ClazzName = GetRequestMap("clazzName", String.Empty);
                if (clazz.ClazzName.IsNullOrWhiteSpace())
                {
                    throw new Exception("请输入班级名称！");
                }
                _clazzService.Update(clazz);
                return Content("true");
            }
            catch (Exception e)
            {
                return Content("err:" + e.Message);
            }
        }


    }
}